﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using LSEntity;

namespace LSBLL
{
    public class MyProfileProvider : ProfileProvider, IRequiresSessionState
    {
        private const string ErrInvalidParameter = "Invalid Profile parameter:";
        private static string _applicationName = "HighScore";
        public override string ApplicationName
        {
            get
            {
                return _applicationName;
            }
            set
            {
                _applicationName = value;
            }
        }
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (config == null)
                throw new ArgumentNullException("config");
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "HighScore Session Profile Provider");
            }
            if (string.IsNullOrEmpty(name))
                name = "MyProfileProvider";
            if (config["applicationName"] != null && !string.IsNullOrEmpty(config["applicationName"].Trim()))
                _applicationName = config["applicationName"];
            base.Initialize(name, config);
        }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            foreach (SettingsPropertyValue pv in collection)
            {
                //if (pv.PropertyValue != null)
                //{
                HttpContext.Current.Session[pv.Property.Name] = pv.PropertyValue;
                //}
            }
        }
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            var svc = new SettingsPropertyValueCollection();
            foreach (var pv in from SettingsProperty prop in collection select new SettingsPropertyValue(prop))
            {
                pv.PropertyValue = HttpContext.Current.Session[pv.Property.Name] != null ? Convert.ChangeType(HttpContext.Current.Session[pv.Property.Name], pv.Property.PropertyType) : null;
                svc.Add(pv);
            }
            return svc;
        }
        #region Not Implemented
        public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int DeleteProfiles(string[] usernames)
        {
            throw new NotImplementedException();
        }

        public override int DeleteProfiles(ProfileInfoCollection profiles)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }
        #endregion
    }

    public class MyProfileBase : ProfileBase
    {
        /// <summary>
        /// 当前登录用户
        /// </summary>
        [SettingsAllowAnonymous(true)]
        [ProfileProvider("MyProfileProvider")]
        public MemberEntity CurrentMember
        {
            get
            {
                if (base[WebConfig.AuthCookieName] == null && HttpContext.Current.User.Identity.IsAuthenticated)
                    base[WebConfig.AuthCookieName] = MemberBLL.MemberGet(HttpContext.Current.User.Identity.Name);
                return base[WebConfig.AuthCookieName] as MemberEntity;
            }
            set
            {
                base[WebConfig.AuthCookieName] = value;
            }
        }

        /// <summary>
        /// 当前试卷
        /// </summary>
        [SettingsAllowAnonymous(true)]
        [ProfileProvider("MyProfileProvider")]
        public TestOTCurrent CurrentTestOT
        {
            get
            {
                return base[WebConfig.TestOTCookieName] as TestOTCurrent;
            }
            set
            {
                base[WebConfig.TestOTCookieName] = value;
            }
        }
    }
}
